import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/index";
import { getToken } from "../utils/auth";

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

//入库
import inbound from "./Inbound";
//出库
import outbound from "./outbound";
//配置
import config from "./config";
//计费
import bill from "./bill";
//管理
import manage from "./manage";
//执行
import execute from "./execute";
//工作站
import workstation from "./workstation";
//波次
import wave from "./wave";
// 导出入功能模块
import transmit from "./transmit";
//安全
import security from "./security";
//任务调度
import dispatchTask from "./dispatchTask";

import application from "./application";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: (resolve) =>
      require(["../components/page/login/Login.vue"], resolve),
  },
  {
    path: "/index",
    name: "index",
    component: (resolve) => require(["../components/page/index.vue"], resolve),
    children: [
      {
        path: "/static",
        name: "static",
        component: (resolve) =>
          require(["../components/page/static/index.vue"], resolve),
        meta: { title: "other.home", flag: false },
      },
      ...inbound,
      ...outbound,
      ...config,
      // ...bill,
      ...manage,
      ...execute,
      ...workstation,
      ...wave,
      ...security,
      ...dispatchTask,
      ...transmit,
      ...application,
      {
        path: "/app/birt/list",
        name: "birt",
        component: (resolve) =>
          require(["../components/page/birt/birt.vue"], resolve),
        meta: { title: "报告", flag: false },
      },
      {
        path: "/birtDeawer",
        name: "birtDeawer",
        component: (resolve) =>
          require(["../components/page/birt/deawer/Deawer.vue"], resolve),
        meta: { title: "报告", flag: true },
      },
      {
        path: "/app/chart/list",
        name: "chart",
        component: (resolve) =>
          require(["../components/page/chart/chart.vue"], resolve),
        meta: { title: "图表", flag: false },
      },
      {
        path: "/chartDeawer",
        name: "chartDeawer",
        component: (resolve) =>
          require(["../components/page/chart/deawer/Deawer.vue"], resolve),
        meta: { title: "图表", flag: true },
      },
      {
        path: "/iframe",
        name: "iframe",
        component: (resolve) =>
          require(["../components/page/Iframe.vue"], resolve),
        meta: { title: "外围程序", flag: false },
      },
      {
        path: "/app/rtxcustquery",
        name: "RTXCUSTQUERY",
        component: (resolve) =>
          require([
            "../components/page/outbound/periphery/rtxCustQuery/rtxCustQuery.vue",
          ], resolve),
        meta: { title: "报表查询", flag: false },
      },
      {
        path: "/app/customimport",
        name: "customimport",
        component: (resolve) =>
          require([
            "../components/page/outbound/periphery/customImport/customImport.vue",
          ], resolve),
        meta: { title: "自定义导入", flag: false },
      },
    ],
  },
  {
    path: "/",
    redirect: "/index",
  },
];

const router = new VueRouter({
  // mode: "history",
  routes,
  base: process.env.BASE_URL,
});

const whiteList = ["/login"];

router.beforeEach(async (to, from, next) => {
  if (getToken()) {
    if (to.path === "/login") {
      next({ path: "/index" });
    } else {
      const hasMenuInfo = store.getters.menuInfo;
      if (hasMenuInfo) {
        next();
      } else {
        try {
          await store.dispatch("getMenuInfo");
          next({ ...to, replace: true });
        } catch (error) {
          // next(`/login?redirect=${to.path}`);
          next();
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
    }
  }
});
export default router;
